const {
  eventBus
} = getApp().globalData;
import {
  getDicts
} from "../../../api/common";
import {
  AddFeedback
} from "../../../api/feedback";
import {
  getShowFeedback
} from "../../../api/cm"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showData: {},
    name: '',
    content: '',
    phone: '',
    checked: false,
    radio: '1',
    list: null,
    rows: null,
    isData: false,
    pageNum: 1,
    space:" "
  },
  gettype() {
    getDicts({
      data: {
        key: 'feedback_type'
      },
      callBack: res => {
        this.setData({
          list: res.data,
          radio: res.data[0].dictValue
        })
        //  console.log(res.data)
        //  console.log(list)
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 添加事件监听器

    eventBus.on("shortcutItem", this.handleShortcutItem);
    this.gettype();
    this.getShowFeedback();
  },

  onUnload() {
    // 移除事件监听器
    eventBus.off("shortcutItem", this.handleShortcutItem);
  },

  handleShortcutItem(data) {
    this.setData({
      showData: data,
    });
  },
  goBack() {
    wx.navigateBack()
  },

  onCheckboxChange(e) {

    this.setData({
      name: '匿名'
    });

  },
  //提交反馈
  submit(e) {
    // console.log(e);
    // console.log(e.detail.value.phone)
    let {
      content,
      phone,
      name
    } = e.detail.value
    if (phone == '' || name == '' || content == '' || this.data.radio == '') {
      wx.showModal({
        title: '提示',
        content: '请把信息填写完整'
      })
      return;
    }

    AddFeedback({
      data: {
        "name": name,
        "phone": phone,
        "content": content,
        "type": this.data.radio
      },
      callBack: (res) => {
        console.log(res)
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 500,
          success(res) {
            setTimeout(() => {
              wx.navigateBack({
                delta: 0,
              })
            }, 500);
          }
        })


      }

    })
  },
  getShowFeedback() {
    wx.showLoading({
      title: '加载中',
    });
    getShowFeedback({
      data: {
        "pageNum": this.data.pageNum,
        "pageSize": 5
      },
      callBack: (res) => {
        console.log(res.rows)
        let oldData
        if (!this.data.rows) {
          oldData = []
        } else {
          oldData = this.data.rows
        }
        console.log(oldData)
        let newData = oldData.concat(res.rows.map(item => ({
          ...item, // 复制当前对象的所有属性
     
          createTime:item.createTime.substring(0,10),
          // content:'                      ' +item.content,
          // reply:'                       ' +item.reply
        })))

        this.setData({
          rows:newData
        })
        console.log(this.data.rows)
        if (this.data.rows.length == res.total || res.rows.length < 5) {
          this.setData({
            isData: true
          })
        }
        wx.stopPullDownRefresh();
        wx.hideLoading();

      }

    })
  },
  //反馈类型
  onChangetype(event) {

    this.setData({
      radio: event.detail,
    });
    // console.log(this.data.radio)
  },

  //匿名复选框
  onChange(event) {
    if (!this.data.checked) {
      this.setData({
        checked: event.detail,
        name: '匿名'
      });
    } else {
      this.setData({
        checked: event.detail,
        name: ''
      });
    }

  },
  onReachBottom() {

    if (this.data.isData) return;
    console.log('我触底了');
    this.data.pageNum = this.data.pageNum + 1;
    this.getShowFeedback();

  },
});